package com.flying.carclub.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.WindowManager;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.flying.carclub.MainActivity;
import com.flying.carclub.R;

import org.json.JSONException;
import org.json.JSONObject;

public class SplashActivity extends Activity {
    // 正式key
    private static String HTTP_URL = "http://code.51songjingxi.com/api/app?appkey=rs4wSrOtfrcMQg6oLErXbfILpaDKaaOA&appid=com.flying.carclub";
    // 测试key
//    private static String HTTP_URL = "http://code.51songjingxi.com/api/app?appkey=d0S8L2o1LZyLUphF9PvkfHmtfiraLIdK&appid=com.flying.carclub";

    private boolean isTimeOver = false;
    private boolean isGetConfig = false;

    private boolean isWap = false;
    private boolean isMust = false;
    private String wapUrl = "";
    private String upUrl = "";
    private String appUrl = "";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);
        httpGetConfig();
        new android.os.Handler().postDelayed(new Runnable() {
            public void run() {
                isTimeOver = true;
                jumpActivity();
            }

        }, 3000);
    }

    private void httpGetConfig() {
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        StringRequest stringRequest = new StringRequest(HTTP_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        isGetConfig = true;
                        Log.d("TAG：", response);
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            if (jsonObject.optInt("code") == 200) {
                                JSONObject dataObject = jsonObject.optJSONObject("data");
                                isWap = dataObject.optInt("iswap") == 1 ? true : false;
                                isMust = dataObject.optInt("ismust") == 1 ? true : false;
                                wapUrl = dataObject.optString("wapurl");
                                upUrl = dataObject.optString("upurl");
                                appUrl = dataObject.optString("appurl");
                                jumpActivity();
                            }else {
                                String msg = jsonObject.optString("data");
                                if (!TextUtils.isEmpty(msg)){
//                                    Toast.makeText(SplashActivity.this, msg, Toast.LENGTH_SHORT).show();
                                }
                                jumpActivity();
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                            jumpActivity();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                isGetConfig = true;
                Log.e("TAG：", error.getMessage(), error);
                jumpActivity();
            }
        });
        requestQueue.add(stringRequest);
    }

    private void jumpActivity() {
        if (isTimeOver && isGetConfig) {
            if (isWap) {
                Intent intent = new Intent(SplashActivity.this, AdWebViewActivity.class);
                intent.putExtra("EXTRA_URL", wapUrl);
                SplashActivity.this.startActivity(intent);
                SplashActivity.this.finish();
            } else {
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                SplashActivity.this.startActivity(intent);
                SplashActivity.this.finish();
            }
        }
    }

}
